package com.maxmlr.music.common.bean;

import com.maxmlr.music.common.enums.ErrorCode;
import com.maxmlr.music.common.enums.IErrorCode;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class ResultBean<T> extends Bean {
    private static final long serialVersionUID = 1L;

    protected int code = ErrorCode.SUCCESS.getCode();
    protected String msg = ErrorCode.SUCCESS.getMsg();
    protected T data;
    protected Date dateTime = new Date();


    public ResultBean(T data) {
        this.data = data;
    }

    public ResultBean(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> ResultBean<T> ok(T data) {
        return new ResultBean<>(data);
    }

    public static <T> ResultBean<T> ok(T data, String msg) {
        ResultBean<T> resultBean = new ResultBean<>(data);
        resultBean.setMsg(msg);
        return resultBean;
    }

    public static<T> ResultBean<T> error(IErrorCode errorCode) {
        return new ResultBean<>(errorCode.getCode(), errorCode.getMsg());
    }

    public static<T> ResultBean<T> error(IErrorCode errorCode, Object[] objects) {
        return new ResultBean<>(errorCode.getCode(), errorCode.getMsg());
    }

    public static<T> ResultBean<T> error(String msg) {
        return new ResultBean<>(ErrorCode.ERROR.getCode(), msg);
    }

}
